package leetcodev1.树;

import java.util.*;

public class LeetCode199 {

    //层次遍历 每次选最右的元素
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            TreeNode poll = null;
            for (int i = 0; i < size; i++) {
                poll = queue.poll();

                if (poll.left != null) {
                    queue.add(poll.left);
                }

                if (poll.right != null) {
                    queue.add(poll.right);
                }
            }

            ret.add(poll.val);
        }
        return ret;
    }
}
